# -*- mode:Makefile -*-
#
# To generate all surface data sets, run:
# make -f Makefile.data all
#
# To generate a single dataset, run make with the name of the rule you
# want to build. For example, to generate the crop data set for 1x1_numaIA:
#
# make -f Makefile.data crop-numa
#
# NOTE: The default behavior is to parallelize data set creation using
# the batch system by submitting jobs to the batch queue (on cheyenne).
# On yellowstone we submit to an interactive queue in the
# background. Standard out and standard error are redirected to a text
# file. To change this behavior, you can comment out the BATCHJOBS and
# BACKGROUND variables and replace them with empty variables.
#
# WARNING: Do not put more than one mksurfdata call per rule. output
# redirection is based on the rule name, and multiple rules will over
# write the previous output or incomprehensively merge output from
# simultaneously running jobs.
#
# Note that we typically use -no_surfdata in rules for transient files, having
# separate rules to make the year-1850 and year-2000 surface datasets. This
# results in extra processes, but ensures that the surface datasets have the
# correct name (rather than having 'hist' or 'rcpXXX' in their file name).
#

# Set up special characters
null  :=

# Set a few things needed for batch handling
PROJECT = $(shell cat $(HOME)/.cesm_proj)
LOGOUT = $@.stdout.txt
PWD = $(shell pwd)

# Setup batch handling for either cheyenne or yellowstone
# Determine what to use by machine hostname
BATCHJOBS_ys = execgy
# Send to regular  queue for 2 processors with extra memory, combine stdout/stderr output to log file, and send email on abort or exit
BATCHJOBS_ch = qsub -A $(PROJECT) -q regular -l select=1:ncpus=2:mem=110GB -l walltime=4:00:00 -j oe -N $(LOGOUT) -m ae -- 
HOST = $(shell hostname)
FINDCH = $(findstring cheyenne,$(HOST))
ifeq ($(FINDCH),$(null))
   ifeq ($(PROJECT),$(null))
      $(error Can NOT find PROJECT number from ~/.cesm_proj file create it and try again)
   endif
   BATCHJOBS = $(BATCHJOBS_ys)
   BACKGROUND = &> $(LOGOUT) &
else
   BATCHJOBS = $(BATCHJOBS_ch)
   BACKGROUND = -rundir $(PWD)
endif

MKSURFDATA = $(BATCHJOBS) $(PWD)/mksurfdata.pl

# f19 and f09 are standard resolutions, f10 is used for testing, f45 is used for FATES
# ne30np4 is standard resolution for SE dycore in CAM, C96 is standard for fv3 dycore
# The ne30np4 series (including pg2, pg3, pg4) are standard for SE dycore
# The variable resolution grids for ARCTIC, ARCTICGRIS and CONUS are also standard
STANDARD_RES_NO_CROP = 0.9x1.25,1.9x2.5,10x15
STANDARD_RES = 0.9x1.25,1.9x2.5,10x15,4x5,ne30np4,C96,ne30pg2,ne30pg3,ne30pg4,ne120np4pg3,ne0np4ARCTICGRISne30x8,ne0np4ARCTICne30x4,ne0np4CONUSne30x8

# For future CMIP6 scenarios: SSP-RCP's
FUTURE_RES = 0.9x1.25,1.9x2.5,10x15
# For historical transient cases (TRY TO KEEP THIS LIST AS SHORT AS POSSIBLE)
TRANS_RES = 0.9x1.25,1.9x2.5,10x15,ne30np4,ne0np4ARCTICGRISne30x8,ne0np4ARCTICne30x4,ne0np4CONUSne30x8

# ne120np4 is for high resolution SE dycore, ne16 is for testing SE dycore
# T42 is for SCAM
# f05 is needed for running full chemistry model
# nldas is for NWP working with WRF
STANDARD = \
	global-present \
	global-present-nldas \
	global-present-T42 

TROPICS = \
	crop-tropics-present \
	crop-tropics-historical \
	crop-tropics-transient

CROP = \
	crop-global-present \
	crop-global-present-ne16np4 \
	crop-global-present-ne120np4 \
	crop-numa-present \
	crop-numa-historical \
	crop-smallville \
	crop-smallville-historical \
	crop-global-historical \
	crop-global-transient \
	crop-global-future

all : standard tropics crop urban landuse-timeseries

DEBUG:
	@echo "HOST       := $(HOST)"
	@echo "PROJECT    := $(PROJECT)"
	@echo "BATCHJOBS  := $(BATCHJOBS)"
	@echo "BACKGROUND := $(BACKGROUND)"
#
# standard
#
standard : $(STANDARD)

global-present : FORCE
	$(MKSURFDATA) -no-crop -vic -glc_nec 10 -y 2000 -res $(STANDARD_RES_NO_CROP) $(BACKGROUND)

# T42 is needed for SCAM
global-present-T42 : FORCE
	$(MKSURFDATA) -no-crop -glc_nec 10 -y 2000 -res 64x128 $(BACKGROUND)

global-present-nldas : FORCE
	$(MKSURFDATA) -no-crop -hirespft -glc_nec 10 -y 2005 -res 0.125nldas2 $(BACKGROUND)

#
# tropics
#
tropics : $(TROPICS)

crop-tropics-present : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -res 5x5_amazon,1x1_brazil $(BACKGROUND)

crop-tropics-historical : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -res 1x1_brazil $(BACKGROUND)

crop-tropics-transient : FORCE
	$(MKSURFDATA) -glc_nec 10 -no_surfdata -y 1850-2000 -res 1x1_brazil $(BACKGROUND)

#
# crop
#
crop : $(CROP)

crop-global-present : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -r $(STANDARD_RES) $(BACKGROUND)

crop-global-present-0.125 : FORCE
	$(MKSURFDATA) -hirespft -glc_nec 10 -y 2000 -r 0.125x0.125 $(BACKGROUND)

crop-global-present-f05 : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850,2000 -res 0.47x0.63 $(BACKGROUND)

crop-numa-present : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -r 1x1_numaIA $(BACKGROUND)

crop-numa-historical : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -r 1x1_numaIA $(BACKGROUND)

crop-smallville : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -r 1x1_smallvilleIA \
		-pft_idx 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78 \
		-pft_frc 6.5,1.5,1.6,1.7,1.8,1.9,1.5,1.6,1.7,1.8,1.9,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5 \
		$(BACKGROUND)

crop-global-present-ne16np4 : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -res ne16np4 $(BACKGROUND)

crop-global-present-ne120np4 : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 2000 -res ne120np4 $(BACKGROUND)

# Note that the smallville 1850 dataset is entirely natural vegetation. This
# facilitates testing a transient case that starts with no crop, and then later
# adds crop (to make sure that it works properly to add crop in a grid cell
# where there used to be no crop).
crop-smallville-historical : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -r 1x1_smallvilleIA -pft_idx 13 -pft_frc 100 $(BACKGROUND)

# Setup the historical case for SSP5-8.5 so that historical can be used to go into the future.
crop-global-historical : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -ssp_rcp SSP5-8.5 -res $(STANDARD_RES) $(BACKGROUND)

crop-global-historical-f05 : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -r 0.47x0.63 $(BACKGROUND)

crop-global-historical-ne120np4 : FORCE
	$(MKSURFDATA) -glc_nec 10 -y 1850 -res ne120np4 $(BACKGROUND)

crop-global-transient: FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2000 -res $(TRANS_RES) $(BACKGROUND)

crop-global-transient-ne120np4 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2000 -res ne120np4 $(BACKGROUND)

crop-global-transient-f05 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2000 -res 0.47x0.63 $(BACKGROUND)

#
# Crop with future scenarios
#
crop-global-future : crop-global-SSP1-2.6 crop-global-SSP3-7.0 crop-global-SSP5-3.4 crop-global-SSP2-4.5 \
                     crop-global-SSP1-1.9 crop-global-SSP4-3.4 crop-global-SSP4-6.0 crop-global-SSP5-8.5

crop-global-SSP1-2.6 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP1-2.6 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP3-7.0 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP3-7.0 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP5-3.4 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP5-3.4 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP2-4.5 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP2-4.5 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP1-1.9 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP1-1.9 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP4-3.4 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP4-3.4 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP4-6.0 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP4-6.0 -res $(FUTURE_RES) $(BACKGROUND)

crop-global-SSP5-8.5 : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-2100 \
	-ssp_rcp SSP5-8.5 -res $(FUTURE_RES) $(BACKGROUND)

#
# urban
#
urban : urban-present urban-alpha

urban-present : FORCE
	$(MKSURFDATA) -y 2000 -no-crop -glc_nec 10 -r 1x1_camdenNJ,1x1_vancouverCAN,1x1_mexicocityMEX $(BACKGROUND)

# NOTE(bja, 2015-01) skip abort on invalid data necessary as of 2015-01. See
# /glade/p/cesm/cseg/inputdata/lnd/clm2/surfdata_map/README_c141219
urban-alpha : FORCE
	$(MKSURFDATA) -y 2000 -no-crop -glc_nec 10 -r 1x1_urbanc_alpha -urban_skip_abort_on_invalid_data_check $(BACKGROUND)


#
# landuse timeseries
#
landuse-timeseries : landuse-timeseries-smallville

landuse-timeseries-smallville : FORCE
	$(MKSURFDATA) -no_surfdata -glc_nec 10 -y 1850-1855 -r 1x1_smallvilleIA \
		-pft_idx 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78 \
		-pft_frc 6.5,1.5,1.6,1.7,1.8,1.9,1.5,1.6,1.7,1.8,1.9,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5 \
		-dynpft single_point_dynpft_files/landuse_timeseries_smallvilleIA_hist_simyr1850-1855.txt \
		$(BACKGROUND)

#
# clean up the working directory by removing generated files
#
clean : FORCE
	-rm *~

clobber : clean
	-rm surfdata_*.nc surfdata_*.log surfdata_*.namelist

#
# generic rule to force things to happen
#
FORCE :

